package com.homedo.odin.solution.service.bizservice.wap;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.wap.BrandFirmAPPDetailResp;
import com.homedo.odin.solution.enumcode.solution.SolutionStateEnum;
import com.homedo.odin.solution.enumcode.solution.SolutionTypeEnum;
import com.homedo.odin.solution.model.dto.UserSolutionCount;
import com.homedo.odin.solution.model.entity.BrandFirm;
import com.homedo.odin.solution.model.entity.BrandOwner;
import com.homedo.odin.solution.service.BrandFirmServiceImpl;
import com.homedo.odin.solution.service.BrandOwnerServiceImpl;
import com.homedo.odin.solution.service.SolutionServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: donglibing
 * @date: 2018/3/27 17:36
 * @Description:
 */
@Service
public class WapBrandFirmBizService {


    @Autowired
    private BrandFirmServiceImpl brandFirmService;

    @Autowired
    private BrandOwnerServiceImpl brandOwnerService;

    @Autowired
    private SolutionServiceImpl solutionService;

    /**
     * M站_获取品牌商详情
     */
    public BrandFirmAPPDetailResp getBrandFirmDetail(Integer uid) {

        if (!NumberHelper.validatePositive(uid)) {
            throw new BizException(Meta.CODE_451.getCode(), "用户编号不合法!");
        }
        BrandFirmAPPDetailResp brandFirmDetail = new BrandFirmAPPDetailResp();
        //查询该品牌商是否存在,不存在立即返回
        BrandFirm brandFirm = brandFirmService.getBrandFirmByUserId(uid);
        if (brandFirm == null || SolutionStateEnum.AUDITSUCCESS.getDbValue() != brandFirm.getStatus().intValue()) {
            throw new BizException(Meta.CODE_451.getCode(), "该品牌厂商不存在!");
        }
        brandFirmDetail.setUserId(brandFirm.getUserId());
        brandFirmDetail.setUserName(brandFirm.getUserName());
        brandFirmDetail.setBrandId(brandFirm.getBrandId());
        brandFirmDetail.setFirmName(brandFirm.getFirmName());
        brandFirmDetail.setFirmIntroduction(brandFirm.getFirmIntroduction());
        brandFirmDetail.setFirmIcon(brandFirm.getFirmIcon());
        brandFirmDetail.setFirmBanner(brandFirm.getFirmBanner());
        brandFirmDetail.setContact(brandFirm.getContact());
        brandFirmDetail.setContactMobile(brandFirm.getContactMobile());
        brandFirmDetail.setContactEmail(brandFirm.getContactEmail());
        brandFirmDetail.setBusinessLicense(brandFirm.getBusinessLicense());
        brandFirmDetail.setFirmHonor(brandFirm.getFirmHonor());
        brandFirmDetail.setSolutionTotal(0);
        brandFirmDetail.setSolutionViewTotal(0);
        brandFirmDetail.setSolutionDownloadTotal(0);
        BrandOwner brandOwner = brandOwnerService.getBrandOwnerById(brandFirm.getBrandId());
        if (brandOwner != null) {
            brandFirmDetail.setBrandName(brandOwner.getBrandName());
            brandFirmDetail.setBrandIcon(brandOwner.getBrandIcon());
        }
        UserSolutionCount count = solutionService
                .queryUserSolutionCount(uid, 30, SolutionTypeEnum.BRAND_SOLUTION.getDbValue());
        if (count != null && count.getTotal() > 0) {
            brandFirmDetail.setSolutionTotal(count.getTotal());
            brandFirmDetail.setSolutionViewTotal(count.getViewTotal());
            brandFirmDetail.setSolutionDownloadTotal(count.getDownloadTotal());
        }
        return brandFirmDetail;

    }


}
